// Dynare code for ARGEMmin (Dynare version 4.2.0)
// Guillermo Escud�. October 2011.

// ARGEMmin: A DSGE model for a SOE that has systematic intervention in 
// FX market and uses 1 or 2 policy rules and hence 3 general regimes:
// MER: Managed Exchange Rate regime:  2 feedback rules
// FER: Floating Exchange Rate regime: 1 feedback rule (Taylor rule) for the interest rate
// PER Pegged Exchange Rate regime:    1 (possibly feedback) rule for the rate of nominal depreciation

// FOR SIMPLE POLICY RULES OR OPTIMAL SIMPLE RULES (osr):

// For the MER regime:
// Use the 2 simple policy rules

// For the FER regime:
// Replace the second policy rule by equation 'r=r_ss' 

// For the PER regime:
// Replace the first policy rule by equation 'b=b_ss'

// FOR OPTIMAL RULES UNDER COMMITMENT (ramsey_policy):

// Comment out the 2 policy rules
// Comment out 'steady', 'check', 'resid', and 'stoch_simul' 
// Use 'planner_objective' and 'ramsey_policy'
// For MER regime use option 'instruments=(ii,delta)'
// For FER regime use option 'instruments=(ii)' and the equation 'r=r_ss' 
// For PER regime use option 'instruments=(delta)' and the equation 'b=b_ss'

close all;

// ENDOGENOUS VARIABLES

var ii delta r b e piC Y d  C pii Q mc Deltta pC pStar varphiM tauM m X 
TB w z_piStar z_piStarX z_G z_epsilon z_iStar z_phiStar GammaP PsiP real_ii 
tax qf CA N Utility dii ddelta gammaD gammaM gammaR  ;

// EXOGENOUS VARIABLES

varexo eps_epsilon eps_G eps_iStar eps_phiStar eps_piStar eps_piStarX ; 

// PARAMETERS

parameters 

// Monetary and XR simple policy rules
h_0 h_1 h_2 h_3               //Interest rate Simple Rule 
k_0 k_1 k_2 k_3 k_4           //Exchange rate Simple Rule 

//Weights for ramsey ad-hoc CB loss function
omega_piC omega_Y omega_e omega_r omega_dii omega_ddelta

// Persistence parameters in shock variables          
rhoPiStar rhoPiStarX rhoG rhoEpsilon rhoiStar rhoPhiStar rhoPiStarNX

// Great ratios  
gammaR_ss gammaD_ss gammaM_ss pCCtoy mtoy btoy gtopCC tbetoy 

// General                                                                           
piT betta sigmaC a_D thetta thettaC sigmaN alfa bA bX kappaX 
varepsilon_L varepsvarphi_D alpha_1 alpha_2 tau_D 
xiN betta_1 betta_2 betta_3 EC_speed_N EC_speed_X Coint_param

// Steady state values of endogenous variables
Y_ss piStar_ss iStar_ss phiStar_ss pStar_ss  e_ss C_ss d_ss r_ss m_ss b_ss 
mc_ss Q_ss GammaP_ss PsiP_ss N_ss pC_ss X_ss TB_ss Deltta_ss w_ss 
varphiM_ss tauM_ss tax_ss qf_ss Utility_ss ;

// PARAMETER VALUES

// Monetary and XR simple policy rules (these don't affect the SS)
// If k_4<0, h_0+h_1 must be greater than 1 to avoid indeterminacy and unit root 

//MER (Managed XR)
h_0=0.33; h_1=1.26; h_2=0.02;   h_3=0.12; 
k_0=-0.03; k_1=-0.07; k_2=-0.08; k_3=-0.43; k_4=-0.08;      

//FER                                                 
//h_0=0.01; h_1=2.04; h_2=-0.05;   h_3=0.04;               

//PER              
//k_0=-0.26;   k_1=-2.08;   k_2=-2.36;   k_3=-1.09;    k_4=-2.18 ;   

// Calibrated steady state values of endogenous variables
Y_ss=1.443; piStar_ss=1.0; iStar_ss=1.03^0.25; phiStar_ss=1.005^0.25; pStar_ss=1;

// Persistence parameters in shock variables (these don't affect the SS)
rhoEpsilon=0.8; rhoG=0.85; rhoiStar=0.7; rhoPhiStar=0.3;
rhoPiStar=0.20; rhoPiStarX= 0.41 ; rhoPiStarNX=0.18;

piT=1.015;             //Inflation target       
alfa=0.66;             //Probability of non-optimizing price
betta=0.99;            //Intertemporal discount factor                    
sigmaC=1.5;            //Constant relative risk aversion for consumption
sigmaN=0.5 ;           //Constant relative risk aversion for labor 
a_D=0.86;              //Home bias in consumption
thetta=6;              //Elasticity of substitution for domestic goods
thettaC=1.5;           //Elasticity of substitution between domestic and imported bundles
bA=0.5;                //Parameter for exports demand
varepsvarphi_D=2;      //Debt/GDP ratio elasticity of varphiD (in UIP)
varepsilon_L=1.02 ;    //Elasticity of money/consumption function w.r. to ii
gammaD_ss=0.5;         //Household Foreign Debt to GDP ratio
 
//Cointegration in trade prices parameters 
EC_speed_N=0.181; EC_speed_X=-0.255; Coint_param=1.0; 

bX=1/(1-bA);                    // Auxiliary parameter for exports demand
kappaX=bA^(bA/(1-bA));          // Auxiliary parameter for exports demand

// Risk premium parameters and variables 
alpha_2=1/((2/varepsvarphi_D)*(1-betta*iStar_ss*phiStar_ss)+gammaD_ss);
alpha_1=(1-alpha_2*gammaD_ss)^2*(1/(betta*iStar_ss*phiStar_ss)-1);                  
tau_D=1+alpha_1/(1-alpha_2*gammaD_ss);                            

// RER determination
gammaR_ss=0.13;           //CB reserves to GDP ratio
tbetoy=((iStar_ss/piStar_ss)*phiStar_ss*tau_D-1)*gammaD_ss-(iStar_ss/piStar_ss-1)*gammaR_ss;
f_e = @(e_ss) (kappaX*(e_ss*pStar_ss)^bX*(a_D+(1-a_D)*(e_ss)^(1-thettaC))-(1-a_D)*e_ss^(1-thettaC))/a_D-tbetoy;
e_ss=fzero(f_e,0.595);                       
pC_ss=(a_D+(1-a_D)*(e_ss)^(1-thettaC))^(1/(1-thettaC));    

// Transactions costs parameters and variables 
gammaM_ss=0.095522;        //money/consumption ratio
betta_3=160;               //Should be >1/(varepsilon_L*(piT/betta-1))-1 
betta_2=(1/gammaM_ss)*(1/(((betta_3+1)*varepsilon_L*(piT/betta-1))-1));                        
betta_1=(1-betta/piT)*((1+betta_2*gammaM_ss)^(betta_3+1))/(betta_2*betta_3);          
tauM_ss=1+betta_1/(1+betta_2*gammaM_ss)^betta_3;
varphiM_ss=1+(tauM_ss-1)*(1+betta_3*((betta_2*gammaM_ss)/(1+betta_2*gammaM_ss))); 

// Other great ratios
gtopCC=0.19;           //Gov expenditure to private consumption ratio
pCCtoy=(1-kappaX*(e_ss*pStar_ss)^bX)*(pC_ss^(1-thettaC)/(a_D*(1+gtopCC)*(tauM_ss)));
mtoy=gammaM_ss*pCCtoy;     //money/GDP ratio
btoy=gammaR_ss-mtoy;       //bonds/GDP ratio

// Steady state values of remaining endogenous variables
Deltta_ss=((1-alfa)/((1-alfa*piT^(thetta))))*((1-alfa*piT^(thetta-1))/(1-alfa))^(thetta/(thetta-1));
Q_ss=Y_ss*(1-(kappaX/bX)*(e_ss*pStar_ss)^bX);
N_ss=Q_ss*Deltta_ss;
C_ss=pCCtoy*Y_ss/pC_ss;         
GammaP_ss=((Q_ss/(pC_ss*C_ss^sigmaC))/(1-betta*alfa*piT^(thetta-1)));
PsiP_ss=GammaP_ss/(((1-alfa*piT^(thetta-1))/(1-alfa))^(1/(thetta-1)));
mc_ss=PsiP_ss*(1-betta*alfa*((piT)^thetta))/((thetta/(thetta-1))*(Q_ss/(pC_ss*C_ss^sigmaC)));    
w_ss=mc_ss;
xiN=w_ss/(N_ss^sigmaN*pC_ss*C_ss^sigmaC*(varphiM_ss));
X_ss=Y_ss*kappaX*(e_ss*pStar_ss)^bX;
r_ss=gammaR_ss*Y_ss/e_ss;
b_ss=btoy*Y_ss;
qf_ss=((iStar_ss)-1/(piT/piStar_ss))*(e_ss*r_ss/piStar_ss)-(piT/betta-1)*(b_ss/piT);
tax_ss=gtopCC*tauM_ss*pC_ss*C_ss-qf_ss;
TB_ss=tbetoy*Y_ss/e_ss;
d_ss=gammaD_ss*Y_ss/e_ss;
m_ss=mtoy*Y_ss;
Utility_ss=(1/(1-sigmaC))*C_ss^(1-sigmaC)-(xiN/(1+sigmaN))*N_ss^(1+sigmaN);

//Weights for CB ad-hoc loss function (for 'ramsey')
omega_piC=100; omega_Y=1; omega_e=1; omega_r=1; omega_dii=50; omega_ddelta=50 ;


// MODEL EQUATIONS

model;

// Simple policy rules (eliminate both for ramsey in MER regime)

// 1 Taylor rule

ii=(piT/betta)^(1-h_0)*(ii(-1))^h_0*(piC/piT)^h_1*(Y/Y_ss)^h_2*(e/e_ss)^h_3;

//For Pegged Exchange Rate (PER) policy use instead:
//b=b_ss;

// 2 Exchange rate depreciation rule:

delta=(piT/piStar_ss)^(1-k_0)*delta(-1)^k_0*(piC/piT)^k_1*(Y/Y_ss)^k_2*(e/e_ss)^k_3*(gammaR/gammaR_ss)^k_4;

//For Floating Exchange Rate (FER) policy use instead:
//r=r_ss;

// 3 Central Bank balance sheet

b=e*r-m;

// 4 Money market clearing

m=(1/betta_2)*(((betta_1*betta_2*betta_3/(1-1/ii))^(1/(betta_3+1)))-1)*pC*C;    

// 5 Trade Balance (in foreign currency)

TB=(1/(a_D*e))*(pC^(1-thettaC)*X-(1-a_D)*e^(1-thettaC)*Y);

// 6 Current account (in foreign currency)

CA=(exp(z_iStar(-1))/exp(z_piStar)-1)*r(-1)-((exp(z_iStar(-1))/exp(z_piStar))*exp(z_phiStar(-1))*(1+alpha_1/(1-alpha_2*gammaD(-1)))-1)*d(-1)+TB;

// 7 Balance of Payments  (in foreign currency)

r=d+CA+r(-1)-d(-1);

// 8 Exports (in foreign currency)

X=Y*kappaX*(e*pStar)^bX;

// 9 RER identity

e=e(-1)*delta*exp(z_piStar)/pii;

// 10  Risk-adjusted uncovered interest parity 

ii=exp(z_iStar)*exp(z_phiStar)*(1+alpha_1/(1-alpha_2*gammaD)^2)*delta(+1); 

// 11 Labor market clearing

w=xiN*N^sigmaN*pC*C^sigmaC*(varphiM);

// 12 Hours worked

N=Q*Deltta/exp(z_epsilon);

// 13 Domestic goods market clearing

Q=Y-(1-bA)*X;

// 14 GDP DEFINITION

Y=a_D*(exp(z_G)*tauM)*pC^thettaC*C+X;         

// 15 Dynamics of Price dispersion

Deltta=alfa*pii^thetta*Deltta(-1)+(1-alfa)*((1-alfa*pii^(thetta-1))/(1-alfa))^(thetta/(thetta-1));

// 16-18 Domestic inflation Phillips equation 

GammaP=PsiP*((1-alfa*pii^(thetta-1))/(1-alfa))^(1/(thetta-1));

GammaP=Q/(pC*C^sigmaC)+betta*alfa*(pii(+1))^(thetta-1)*GammaP(+1);       

PsiP=(thetta/(thetta-1))*(Q/(pC*C^sigmaC))*mc+betta*alfa*((pii(+1))^thetta)*PsiP(+1);    

// 19 Real Marginal Cost

mc=w/exp(z_epsilon);

// 20 Transactions cost (net)

tauM=1+betta_1/(1+betta_2*gammaM)^betta_3;

// 21 Transactions cost effect on the Euler equation (gross)

varphiM=1+(tauM-1)*(1+betta_3*((betta_2*gammaM)/(1+betta_2*gammaM))); 

// 22 Consumption relative price

pC^(1-thettaC)=a_D+(1-a_D)*e^(1-thettaC);   

// 23 Consumption inflation identity

piC=pii*pC/pC(-1);    

// 24 Euler equation

C^(-sigmaC)/(varphiM)=betta*ii*(C(+1)^(-sigmaC)/piC(+1))/(varphiM(+1));

// 25 External Terms of Trade

pStar=pStar(-1)*exp(z_piStarX)/(exp(z_piStar)^Coint_param);     

// 26-27 Cointegrated RW prices

z_piStarX=z_piStarX(-1)*rhoPiStarX+log(pStar(-1))*EC_speed_X+(eps_piStarX);

z_piStar=z_piStar(-1)*rhoPiStar+log(pStar(-1))*EC_speed_N+z_piStar(-1)*rhoPiStarNX+(eps_piStar);

//TO GET THE SAME RESULTS AS IN THE PAPER TABLES USE THESE (WHICH CONTAIN AN ERROR):
//z_piStarX=z_piStarX(-1)*rhoPiStarX+z_piStar(-1)*rhoPiStarNX+log(pStar(-1))*EC_speed_X+(eps_piStarX);
//z_piStar=z_piStar(-1)*rhoPiStar+log(pStar(-1))*EC_speed_N+(eps_piStar);

// 28 Government expenditure shock 

z_G=(z_G(-1))*rhoG+log(1+gtopCC)*(1-rhoG)+(eps_G);

// 29 Transitory productivity shock 

z_epsilon=z_epsilon(-1)*rhoEpsilon+(eps_epsilon);

// 30 RW riskfree interest rate shock 

z_iStar=(z_iStar(-1))*rhoiStar+log(iStar_ss)*(1-rhoiStar)+(eps_iStar); 

// 31 RW Financing liquidity shock 

z_phiStar=z_phiStar(-1)*rhoPhiStar+log(phiStar_ss)*(1-rhoPhiStar)+(eps_phiStar);

// 32-33 Policy instruments differences for ramsey

dii=ii-ii(-1);

ddelta=delta-delta(-1);


// EXTRAS 

// 34 Real interest rate

real_ii=ii/piC(+1);

// 35 Tax collection

tax=(exp(z_G)-1)*tauM*pC*C-qf;

// 36 Quasi-fiscal surplus

qf=(exp(z_iStar(-1))-1/delta)*(e*r(-1)/exp(z_piStar))-(ii(-1)-1)*(b(-1)/pii);

// 37 Period Utility 

Utility=(1/(1-sigmaC))*(C)^(1-sigmaC) - (xiN/(1+sigmaN))*(N)^(1+sigmaN);

// 38-40 Great ratios

gammaD=e*d/Y;

gammaM=m/(pC*C);

gammaR=e*r/Y;


end;

// Reminder: steady, check and resid can�t be used with ramsey
//steady;
//check;
//resid;    

shocks;
var eps_epsilon;        stderr  0.01 ;   
var eps_G;              stderr  0.03 ;  
var eps_iStar;          stderr  0.0046 ;    
var eps_phiStar;        stderr  0.05 ; 
var eps_piStar;         stderr  0.0295 ;   
var eps_piStarX;        stderr  0.0424 ;   
end;

steady_state_model;
ii=piT/betta; delta=piT/piStar_ss; r=r_ss; b=b_ss; e=e_ss; piC=piT; Y=Y_ss; 
pStar=pStar_ss; z_G=log(1+gtopCC); z_epsilon=log(1); z_piStar=log(piStar_ss);  
z_iStar=log(iStar_ss); z_phiStar=log(phiStar_ss); z_piStarX=log(piStar_ss); 
d=d_ss; GammaP=GammaP_ss; PsiP=PsiP_ss; C=C_ss; m=m_ss; N=N_ss; Deltta=Deltta_ss; 
pii=piT; Q=Q_ss;  w=w_ss;  mc=mc_ss; pC=pC_ss; varphiM=varphiM_ss;
tauM=tauM_ss; X=X_ss; TB=TB_ss;  real_ii=ii/piC; tax=tax_ss; qf=qf_ss; 
CA=0; dii=0; ddelta=0; gammaD=gammaD_ss; gammaM=gammaM_ss; 
Utility=Utility_ss;  gammaR=gammaR_ss;
end;

stoch_simul(order=1,irf=0) ii delta e d r C piC Y pii  ;

// FOR OPTIMAL SIMPLE RULES: //////////////////////////////////////////////

//optim_weights;
//piC 100; Y 1; e 1; r 1; dii 50; ddelta 50;
//Utility 1000;
//end;

//osr_params  h_0 h_1 h_2 h_3 k_0 k_1 k_2 k_3 k_4 ;   //For MER
//osr_params  h_0 h_1 h_2 h_3 ;                       //For FER
//osr_params   k_0 k_1 k_2 k_3 k_4 ;                  //For PER

//Provides numerical initialization for the optimization:

//For MER;
//h_0=0.8; h_1=1.5; h_2=0.1; h_3=0.0; k_0=0.0; k_1=0.0; k_2=-0.0; k_3=-0.0; k_4=-0.1;   

//For FER:
//h_0=0.33; h_1=1.25; h_2=0.02; h_3=0.12; 

//For PER:
//k_0=-0.3; k_1=-0.07; k_2=-0.08; k_3=-0.43; k_4=-0.08;   

//Instructs for osr
//osr(order=1,irf=0) piC Y e C gammaD ii b delta r	;
///////////////////////////////////////////////////////////////////////////




// FOR OPTIMAL POLICY WITH COMMITMENT:  ///////////////////////////////////

//planner_objective((omega_piC*(piC-piT)^2+omega_Y*(Y-Y_ss)^2+omega_e*(e-e_ss)^2+omega_r*(r-r_ss)^2+omega_dii*dii^2+omega_ddelta*ddelta^2));

//For MER:
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii,delta),irf=0) ii delta e d r C piC Y pii  ;

//For FER:
//ramsey_policy(planner_discount=0.99,order=1,instruments=(ii),irf=0) ii delta e d r C piC Y pii  ; 

//For PER:
//ramsey_policy(planner_discount=0.99,order=1,instruments=(delta),irf=0) ii delta e d r C piC Y pii  ;

